package com.totoro.project.spboot_rabbitMQ_server;

import com.totoro.project.spboot_rabbitMQ_server.config.MQConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * Created by Administrator on 2017/9/11.
 */
@Service
public class SendService {
    @Resource
    private AmqpTemplate amqpTemplate ;

    public void send(String queue, String msg){
        this.amqpTemplate.convertAndSend(queue, msg);
//        CorrelationData cId = new CorrelationData(UUID.randomUUID().toString()) ;
//        this.amqpTemplate.convertAndSend(EXCHANGE, ROUTEKEY, msg, cId);
    }

    public void sendDefault(String msg){
        this.amqpTemplate.convertSendAndReceive(MQConfig.EXCHANGE, MQConfig.ROUTINGKEY, msg) ;
    }

    /**
     * 采用制定的routingkey发送消息到指定的exchange
     * @param exchange
     * @param routingKey
     * @param msg
     */
    public void send(String exchange, String routingKey, String msg){
        this.amqpTemplate.convertAndSend(exchange, routingKey,msg);
    }

    public void sendFanout(String exchange, String msg){
        this.amqpTemplate.convertAndSend(exchange, msg);
    }
}
